package com.weixing.mall.provider.controller.front;

import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.AuthUtil;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.provider.api.model.vo.DeliveryAddressVo;
import com.weixing.mall.provider.model.dto.DeliveryAddressDto;
import com.weixing.mall.provider.service.IDeliveryAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址管理Api
 *
 * @author  wx
 * @date  2019/11/22 10:01
 * @since  1.0
 */
@RestController
@RequestMapping("front/deliverAddr")
public class FrontDeliveryAddressController extends BaseController {
    @Autowired
    private IDeliveryAddressService deliveryAddressService;

    /**
     * 查询所属的地址
     * @return 地址列表
     */
    @GetMapping("")
    public Result<List<DeliveryAddressVo>> selectByMemberIdList(){
        MemAuth memAuth = AuthUtil.getMemberExt();
        return ResultUtil.success(deliveryAddressService.selectMemberAddrList(memAuth.getMid()));
    }

    /**
     * 新增地址
     *
     * @param dto  参数
     * @return  结果
     */
    @PostMapping("")
    @NoRepeatSubmit
    public Result save(
                       @Validated  @RequestBody DeliveryAddressDto dto){
        MemAuth memAuth = AuthUtil.getMemberExt();
        Long id = deliveryAddressService.create(dto,memAuth.getMid());
        return ResultUtil.success(id);
    }

    /**
     * 查询详情
     * @param id id
     * @return 详情
     */
    @PostMapping("/{id}")
    public Result<DeliveryAddressVo> detail(@PathVariable Long id){
        return ResultUtil.success(deliveryAddressService.selectDetail(id));
    }

    /**
     * 删除地址
     *
     * @param id  id
     * @return 结果
     */
    @PostMapping("del/{id}")
    public Result del(@PathVariable Long id){
        return deliveryAddressService.removeById(id)  ? ResultUtil.success() : ResultUtil.error();
    }
}
